package org.wu.spring.db.integration.compare.infrastructure.persistence;

import jakarta.annotation.Resource;
import org.wu.framework.core.utils.DataTransformUntil;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.web.response.Result;
import org.wu.spring.db.integration.compare.domain.model.sys.user.DbComparisonRepository;
import org.wu.spring.db.integration.compare.domain.model.sys.user.SysUser;
import org.wu.spring.db.integration.compare.enums.DatabaseType;
import org.wu.spring.db.integration.compare.enums.ExecuteType;
import org.wu.spring.db.integration.compare.infrastructure.entity.DbCompareRecordsDO;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 记录 orm执行过程时间
 */
public abstract class DbComparisonRepositoryAbstract implements DbComparisonRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * 存储记录
     *
     * @param databaseType             databaseType 框架
     * @param lambdaTableType 执行类型
     * @param startTime       开始时间
     * @param endTime         结束时间
     * @param success         执行结果
     */
    public void storyRecord(DatabaseType databaseType, ExecuteType lambdaTableType, LocalDateTime startTime, LocalDateTime endTime, boolean success) {
        storyRecord(databaseType, lambdaTableType, startTime, endTime, 1, 1, success);
    }

    /**
     * 存储记录
     *
     * @param lambdaTableType 执行类型
     * @param startTime       开始时间
     * @param endTime         结束时间
     * @param affectsRows     影响行数
     * @param affectsColumn
     * @param success         执行结果
     */
    public void storyRecord(DatabaseType databaseType,
                            ExecuteType lambdaTableType,
                            LocalDateTime startTime,
                            LocalDateTime endTime,
                            Integer affectsRows,
                            Integer affectsColumn,
                            boolean success) {
        DbCompareRecordsDO dbCompareRecordsDO = new DbCompareRecordsDO();
        dbCompareRecordsDO.setDatabaseType(databaseType.name());
        dbCompareRecordsDO.setType(lambdaTableType.name());
        dbCompareRecordsDO.setIsDeleted(false);
        dbCompareRecordsDO.setCreateTime(LocalDateTime.now());
        dbCompareRecordsDO.setUpdateTime(LocalDateTime.now());
        dbCompareRecordsDO.setAffectsRows(affectsRows);
        dbCompareRecordsDO.setAffectsColumn(affectsColumn);
        dbCompareRecordsDO.setSuccess(success);
        // 毫秒
        long durationInMillis = Duration.between(startTime, endTime).toMillis();
        dbCompareRecordsDO.setOperationTime(durationInMillis);
        lazyLambdaStream.upsert(dbCompareRecordsDO);
    }


    /**
     * describe 批量新增
     *
     * @param rowSize    @return {@link Result< List <SysUser>>} 新增后领域对象集合
     * @param columnSize
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/
//    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<List<SysUser>> batchStory(int rowSize, int columnSize) throws Exception {
        return doBatchStory(rowSize, columnSize);
    }


    /**
     * describe 分页查询多个
     *
     * @param rowSize    当前页数
     * @param columnSize
     * @return {@link Result< LazyPage <SysUser>>} 分页领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/
    @Override
    public Result<LazyPage<SysUser>> findPage(int rowSize, int columnSize) throws Exception {
        return doFindPage(rowSize, columnSize);
    }

    public Map<String, Object> getRowData(Integer columnSize,Integer randomId) {
        Map<String, Object> params = new HashMap<>();
        params.put("time", LocalDateTime.now().minusMinutes(randomId).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        for (int i = 0; i < columnSize; i++) {
            params.put("column_name_" + i, DataTransformUntil.simulationBean(BigDecimal.class));
        }
        return params;
    }


    public Map<String, Object> getRowData(Integer columnSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        for (int i = 0; i < columnSize; i++) {
            params.put("column_name_" + i, DataTransformUntil.simulationBean(BigDecimal.class));
        }
        return params;
    }

    /**
     * describe 批量新增
     *
     * @param rowSize    @return {@link Result< List <SysUser>>} 新增后领域对象集合
     * @param columnSize
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/
    protected abstract Result<List<SysUser>> doBatchStory(int rowSize, int columnSize) throws Exception;


    /**
     * describe 分页查询多个
     *
     * @param rowSize    当前页数
     * @param columnSize
     * @return {@link Result<  LazyPage  <SysUser>>} 分页领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    protected abstract Result<LazyPage<SysUser>> doFindPage(int rowSize, int columnSize) throws Exception;

}
